﻿#include "SplashScreen.h"

#include <QLabel>
#include <QPainter>
#include <QPixmap>
#include <QTimer>

///////////////////////////////////////////////////////////////////////////////
// SplashScreen
SplashScreen::SplashScreen(const QPixmap& pixmap)
    : QSplashScreen(pixmap) {}

SplashScreen::SplashScreen(const QString gifname) {
    set_gif(gifname);
}

SplashScreen::~SplashScreen() {}

void SplashScreen::slot_update() {
    m_current_movie = m_move->currentPixmap();
    repaint();
}

void SplashScreen::drawContents(QPainter* painter) {
    QSplashScreen::drawContents(painter);

    // 中心位置
    int x      = (int)((pixmap().width() - m_current_movie.width()) / 2);
    int y      = (int)((pixmap().height() - m_current_movie.height()) / 2);
    int offset = 100;

    // 开始绘制 gif
    painter->drawPixmap(
        x, y + offset, m_current_movie.width(), m_current_movie.height(), m_current_movie);
}

void SplashScreen::set_gif(QString filename) {
    m_move = new QMovie(filename);
    m_move->start();

    QTimer* timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT(slot_update()));
    timer->start(100);
}